#!/bin/bash
# This is the script for running SPECjbb2015

###SHELLPACK preamble specjbb2015-bench 1.00
. $SHELLPACK_INCLUDE/common-detect.sh

install-depends dmidecode

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam --instances	INSTANCES
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

if [ "$INSTALL_ONLY" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/specjbb*
fi

###SHELLPACK check_install_required specjbb-${VERSION}
###SHELLPACK init_complete

export PATH=$SPECJBB_JRE:$PATH
STARTING_HUGEPAGES=`cat /proc/sys/vm/nr_hugepages`

# Ensure everything is where it is expected to be
pushd $SHELLPACK_SOURCES/specjbb2015-${VERSION}-installed > /dev/null || die Failed to cd to specjbb
[ ! -e specjbb2015.jar ] && die No specjbb2015.jar available

# Increase open file limit
ulimit -n 65536

# Run the actual test
for PAGES in $SPECJBB_PAGESIZES; do
	USE_HUGEPAGE=
	case $PAGES in
		default)
			echo Using default pagesize configuration
			;;
		base)
			hugeadm --pool-pages-min DEFAULT:0
			hugeadm --pool-pages-max DEFAULT:0
			USE_HUGEPAGE=
			disable_transhuge
			;;
		huge)
			hugeadm --hard --pool-pages-min DEFAULT:4096M
			USE_HUGEPAGE=$SPECJBB_HUGESWITCH
			disable_transhuge
			;;
		transhuge)
			hugeadm --pool-pages-min DEFAULT:0
			hugeadm --pool-pages-max DEFAULT:0
			USE_HUGEPAGE=
			if [ "$TRANSHUGE_AVAILABLE" = "yes" ]; then
				enable_transhuge
			else
				echo THP support unavailable for transhuge
				continue
			fi
			;;

	esac

	monitor_pre_hook $LOGDIR_RESULTS $P
	mkdir $LOGDIR_RESULTS/$PAGES

	# Use default configuration
	if [ ! -e config/specjbb2015.props.orig ]; then
		cp config/specjbb2015.props config/specjbb2015.props.orig
	fi
	cp config/specjbb2015.props.orig config/specjbb2015.props

	# Run single or multi mode
	EXIT_CODE=$SHELLPACK_SUCCESS
	if [ $INSTANCES -eq 1 ]; then
		set_mmtests_numactl $INSTANCES $INSTANCES
		eval save_rc $MMTESTS_NUMACTL java $USE_HUGEPAGE $SPECJBB_MAXHEAP -jar specjbb2015.jar -m COMPOSITE 2> /dev/null | tee $LOGDIR_RESULTS/$PAGES/specjbb2015.log
		recover_rc
		if [ $EXIT_CODE -ne 0 ]; then
			echo ERROR: java exited with an error
			EXIT_CODE=$SHELLPACK_ERROR
		fi
	else
		TEST_CONTROLLER=`echo $SPECJBB_HEAP_CONTROLLER | sed 's/[^0-9]//g'`
		TEST_BACKEND=`echo $SPECJBB_HEAP_BACKEND | sed 's/[^0-9]//g'`
		TEST_TXI=`echo $SPECJBB_HEAP_TXI | sed 's/[^0-9]//g'`
		TEST_MAX=`echo $SPECJBB_MAXHEAP | sed 's/[^0-9]//g'`

		HEAP_TOTAL=$((TEST_CONTROLLER))
		HEAP_TOTAL=$((HEAP_TOTAL+TEST_BACKEND))
		HEAP_TOTAL=$((HEAP_TOTAL+TEST_TXI*SPECJBB_TXI_PER_INSTANCE))

		SPEC_OPTS="-Dspecjbb.group.count=$INSTANCES -Dspecjbb.txi.pergroup.count=$SPECJBB_TXI_PER_INSTANCE"
		
		# Start controller
		java $SPECJBB_HEAP_CONTROLLER $SPEC_OPTS -jar specjbb2015.jar -m MULTICONTROLLER 2> /dev/null | tee $LOGDIR_RESULTS/$PAGES/specjbb2015.log &
		CONTROLLER_PID=`jobs -p`
		echo $CONTROLLER_PID > $SHELLPACK_TEMP/jvm-instances.pids
		echo Started controller pid: $CONTROLLER_PID
		sleep 10

		# Start the number of instances
		for INSTANCE in `seq 1 $INSTANCES`; do
			set_mmtests_numactl $INSTANCE $INSTANCES

			GROUPID=Group$INSTANCE
			echo Starting JVMs from $GROUPID

			# TXI instances
			for TXI_INSTANCE in `seq 1 $SPECJBB_TXI_PER_INSTANCE`; do
				JVMID=txiJVM$TXI_INSTANCE
				eval $MMTESTS_NUMACTL java $USE_HUGEPAGE $SPECJBB_HEAP_TXI -jar specjbb2015.jar \
					-m TXINJECTOR \
					-G=$GROUPID \
					-J=$JVMID 2> /dev/null > $LOGDIR_RESULTS/$PAGES/txi-${GROUPID}-${JVMID}.log &
				TXI_PID=$!
				echo $TXI_PID >> $SHELLPACK_TEMP/jvm-instances.pids
				echo o TXI instance $GROUPID $JVMID: $TXI_PID
			done

			JVMID=beJVM
			eval $MMTESTS_NUMACTL java $USE_HUGEPAGE $SPECJBB_HEAP_BACKEND -jar specjbb2015.jar \
					-m BACKEND \
					-G=$GROUPID \
					-J=$JVMID \
					2> /dev/null > $LOGDIR_RESULTS/$PAGES/backend-${GROUPID}.log &
			BE_PID=$!
			echo o BE instance $GROUPID: $BE_PID
			echo $BE_PID >> $SHELLPACK_TEMP/jvm-instances.pids
		done

		for INSTANCE_PID in `cat $SHELLPACK_TEMP/jvm-instances.pids`; do
			echo Waiting on pid $INSTANCE_PID to finish.
			wait $INSTANCE_PID
			if [ $? -ne 0 ]; then
				echo WARNING: PID $INSTANCE_PID exited with error
				EXIT_CODE=$SHELLPACK_ERROR
			fi
		done
	fi

	monitor_post_hook $LOGDIR_RESULTS $P
	tar -czf $LOGDIR_RESULTS/$PAGES/result.tar.gz result
	rm -rf result
done

exit $EXIT_CODE
